<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once "wxstore/wxAuthLib.php";
$wxauth = new wxAuthLib();
$authorization_code = $_REQUEST['authorization_code'];
$authorizer_token = $wxauth->getAuthorizationAppid($authorization_code);
//j942gLjYoaP78h_2L9jgIkgDxZ3HvbdHVgLzq2s72qANI7wQJJkkGc9AHSdNW7aMhS5QLV8zIwgg4dOV-RXvF5dm7T9ybhA8TVD8Vd4A7xlBro3HKXvHl3M1CEpDBRImXECeAIDOVT

if($authorizer_token != "")
{
	$authorizer_appid = $authorizer_token['authorization_info'] ['authorizer_appid'];
	$authorizer_info = $wxauth->getAuthorizationInfo($authorizer_appid);
	$authorizer_access_token = $authorizer_token['authorization_info']['authorizer_access_token'];

	if($authorizer_info['authorizer_info']['user_name'] !="")
	{
		saveAuthToken($authorizer_token['authorization_info']); //保存token
		saveAuth($authorizer_info);//保存公众号信息
		$source_file = dirname(__FILE__) ."/wxstore/static/moban.php";
		$dir = dirname(__FILE__) ."/wxstore/".$authorizer_appid."/";
		$to_file = $dir."index.php";
		if(is_dir($dir))
		{
			copy($source_file, $to_file);
		}else
		{
			mkdir($dir);
			copy($source_file, $to_file);
		}
		//授权成功跳转
		$link[] = array('href' => 'wxauth.php', 'text' => '功能管理');
		sys_msg('授权完成', 0, $link);
	}else
	{
		$link[] = array('href' => 'wxauth.php', 'text' => '重新授权');
		sys_msg('授权失败', 0, $link);
	}

}else
{
	$link[] = array('href' => 'wxauth.php', 'text' => '重新授权');
	sys_msg('授权失败', 0, $link);
}

//保存公众号信息
function saveAuth($authorizer_info)
{
	$nick_name = $authorizer_info['authorizer_info']['nick_name'];
	$head_img = $authorizer_info['authorizer_info']['head_img'];
	$service_type_info = $authorizer_info['authorizer_info']['service_type_info']['id'];
	$verify_type_info = $authorizer_info['authorizer_info']['verify_type_info']['id'];
	$user_name = $authorizer_info['authorizer_info']['user_name'];
	$alias = $authorizer_info['authorizer_info']['alias'];
	$business_info = serialize($authorizer_info['authorizer_info']['business_info']);
	$authorizer_appid = $authorizer_info['authorization_info']['authorizer_appid'];
	$func_info = serialize($authorizer_info['authorization_info']['func_info']);
	$qrcode_url = $authorizer_info['authorizer_info']['qrcode_url'];
	if($authorizer_appid != "")
	{
		$has_appid = $GLOBALS['db']->getOne("SELECT COUNT(id) FROM ".$GLOBALS['ecs']->table('wxauthorizer')." WHERE authorizer_appid='".$authorizer_appid."' ");
		if($has_appid > 0)
		{
			$sql = "UPDATE ".$GLOBALS['ecs']->table('wxauthorizer')." SET service_type_info=".$verify_type_info.", verify_type_info=".$verify_type_info.", authorizer_status=1 WHERE authorizer_appid='".$authorizer_appid."' ";
		}else
		{
			include_once(ROOT_PATH . '/includes/cls_image.php');
			$image = new cls_image($_CFG['bgcolor']);
			$qrcode_url_local = $image->get_url_img($qrcode_url, 300, 300, ROOT_PATH.'data/wxauthorizer/', $authorizer_appid);
			$sql = "INSERT INTO ".$GLOBALS['ecs']->table('wxauthorizer')." (seller_id, nick_name,head_img, service_type_info, verify_type_info, user_name, alias, business_info, qrcode_url_local, qrcode_url, authorizer_appid, func_info, add_time, authorizer_status) ".
			   " VALUES(".$_SESSION['seller_id']." ,'".$nick_name."', '".$head_img."', ".$service_type_info.", ".$verify_type_info.", '".$user_name."', '".$alias."', '".$business_info."', '".$qrcode_url_local."', '".$qrcode_url."', '".$authorizer_appid."', '".$func_info."', ".gmtime().", 1) ";
		}
		$GLOBALS['db']->query($sql);
	}

}
//保存公众号token
function saveAuthToken($authorizer_token)
{
	$authorizer_appid = $authorizer_token['authorizer_appid'];
	$authorizer_access_token = $authorizer_token['authorizer_access_token'];
	$expire_time = time() + 7000;
	$authorizer_refresh_token = $authorizer_token['authorizer_refresh_token'];
	$func_info = serialize($authorizer_token['func_info']);
	if($authorizer_appid != "")
	{
		$has_token = $GLOBALS['db']->getOne("SELECT count(id) FROM ".$GLOBALS['ecs']->table('wxauthorizer_token')." WHERE seller_id=".$_SESSION['seller_id']);
		if($has_token > 0)
		{
			$sql = "UPDATE ".$GLOBALS['ecs']->table('wxauthorizer_token')." SET authorizer_access_token='".$authorizer_access_token."', expire_time=".$expire_time.",  ".
			      " authorizer_refresh_token='".$authorizer_refresh_token."', func_info='".$func_info."' WHERE seller_id=".$_SESSION['seller_id'];
		}else
		{
			$sql = "INSERT INTO ".$GLOBALS['ecs']->table('wxauthorizer_token')." (authorizer_appid, authorizer_access_token,expire_time, authorizer_refresh_token, func_info, seller_id) ".
			     " VALUES('".$authorizer_appid."' ,'".$authorizer_access_token."', ".$expire_time.", '".$authorizer_refresh_token."', '".$func_info."', ".$_SESSION['seller_id']." )";
		}
		$GLOBALS['db']->query($sql);
	}
}

?>